<?php

namespace App\Http\Controllers\Login;

use App\Services\Base\Rsp;
use App\Services\Biz\LoginBiz;
use App\Services\Constant\ErrorCode;
use App\Services\Utils\CommonUtil;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;

class LoginController extends Controller
{
    public function anyWebLogin(Request $request) {
        // 参数验证
        $validate = Validator::make($request->all(),[
            'user_account'=>'required',
            'user_password'=>'required',
        ]);

        if($validate->fails()) {
            return Rsp::errorMake(ErrorCode::INVALID_PARAMS,$validate->errors()->first());
        }

        $user_account = $request->get('user_account');
        $user_password = $request->get('user_password');

        $ret = LoginBiz::LoginCheck($user_account,md5($user_password));

        // 用户验证
        if(!$ret) {
            return Rsp::errorMake(1,'用户名或者密码不正确');
        }

        $request->session()->put('user_info',$ret);

        return Rsp::successMake($ret);
    }

    public function anyMobileLogin(Request $request) {
        // 参数验证
        $validate = Validator::make($request->all(),[
            'user_account'=>'required',
            'user_password'=>'required',
            'device_id'=>'required'
        ]);

        if($validate->fails()) {
            return Rsp::errorMake(ErrorCode::INVALID_PARAMS,$validate->errors()->first());
        }

        $user_account = $request->get('user_account');
        $user_password = $request->get('user_password');
        $device_id = $request->get('device_id');

        $user = LoginBiz::LoginCheck($user_account,md5($user_password));

        // 用户验证
        if(!$user) {
            return Rsp::errorMake(1,'用户名或者密码不正确');
        }

        // 获取数据库中session信息
        $session = (array)LoginBiz::getSessionByDeviceIdAndUserUUID($device_id,$user['user_uuid']);

        if(!$session) { // 没有获取到
            $access_token = CommonUtil::getUUID();
            $refresh_token = CommonUtil::getUUID();

            // 向数据库中保存session
            $session = LoginBiz::saveDeviceInfoToSessionTable($device_id,$user['user_uuid'],$access_token,$refresh_token);
            $mix = array_merge($session,$user);
            return Rsp::successMake($mix);
        } else { // 能查找到session
            $mix = array_merge($session,$user);

            session($session['access_token'],$mix);

            return Rsp::successMake($mix);
        }
    }

    public function anyCurrentSession(Request $request) {
        $access_token = $request->get('access_token',null);
        if($access_token) {
            return Rsp::successMake(session($access_token));
        }

        if($request->session()->has('user_info')) {
            return Rsp::successMake($request->session()->get('user_info'));
        }

        return Rsp::errorMake(ErrorCode::TOKEN_TIMEOUT,ErrorCode::getMsg(ErrorCode::TOKEN_TIMEOUT));
    }

    public function anyMobileRefresh() {
        return 'mobile-refresh';
    }
}
